using System;
using Avalonia;
using Avalonia.Animation;
using Avalonia.Controls;

namespace AnimationPage.AttachedProperties;

public sealed class AnimateFadeInProperty : AnimateBaseProperty<AnimateFadeInProperty>
{
    public static readonly AvaloniaProperty<bool> ValueProperty =
        AvaloniaProperty.RegisterAttached<AvaloniaObject, bool>
            ("Value", typeof(AnimateFadeInProperty), coerce: OnCallBack);

    private static bool OnCallBack(AvaloniaObject arg1, bool arg2) => OnCallBack(ValueProperty, arg1, arg2);

    public static bool GetValue(AvaloniaObject d) => d.GetValue<bool>(ValueProperty);

    public static void SetValue(AvaloniaObject d, bool value) => d.SetValue(ValueProperty, value);

    protected override void OnAddTransition(Transitions transitions)
    {
        const float seconds = 1.3f;

        transitions.Add(new DoubleTransition
        {
            Property = Visual.OpacityProperty,
            Duration = TimeSpan.FromSeconds(seconds)
        });
    }

    protected override void DoAnimation(Control control, bool value, bool firstLoad)
    {
        if (value)
        {
            control.Opacity = 1;
            control.IsVisible = true;
        }
        else
        {
            control.Opacity = 0;
            control.IsVisible = false;
        }
    }
}